package com.fenghuajueli.libbasecoreui.videoapi;

import androidx.lifecycle.LifecycleOwner;

import com.fenghuajueli.libbasecoreui.data.entity.video.VideoResponseListEntity;
import com.fenghuajueli.lib_net.common.DefaultObserver;
import com.fenghuajueli.lib_net.common.RetrofitUtils;
import com.fenghuajueli.libbasecoreui.constants.SwitchNetConstants;
import com.fenghuajueli.libbasecoreui.listener.BaseCallBackListener;

import retrofit2.Retrofit;

/**
 * ===========================================================
 * author：songpeng on 2/18/21 17:14
 * email：fenghuajueli@gmail.com
 * ===========================================================
 * 获取视频播单列表数据
 */
public class VideoPlayList {


    public static VideoPlayService videoPlayService;

    public static Retrofit.Builder getDefault() {
        return RetrofitUtils.getRetrofitBuilder(SwitchNetConstants.baseVideoUrl);
    }

    public static VideoPlayService getServices() {
        if (videoPlayService == null) {
            return getDefault().build().create(VideoPlayService.class);
        }
        return videoPlayService;
    }


    /**
     * 获取视频播单列表
     *
     * @param pid 播单的id
     */
    public static void getVideoList(String pid, int pageSize, BaseCallBackListener<VideoResponseListEntity> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        getServices()
                .getVideoList(pid, "2", 500)
                .subscribe(new DefaultObserver<VideoResponseListEntity>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(VideoResponseListEntity response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });

    }

    /**
     * 获取视频播单列表 需要和界面做生命周期绑定
     * @param videoType 0 代表旧接口视频 1代表新接口视频
     * @param lifecycleOwner 当前界面的生命周期监听对象
     * @param pid            播单的id
     */
    public static void getVideoList(LifecycleOwner lifecycleOwner, int videoType,String pid, int pageSize, BaseCallBackListener<VideoResponseListEntity> baseCallBackListener) {
        if (lifecycleOwner == null) {
            return;
        }
        baseCallBackListener.onHandlerStart();
        if (videoType == 0){
            getServices()
                    .getVideoList(pid, "2", 500)
                    .with(lifecycleOwner)
                    .subscribe(new DefaultObserver<VideoResponseListEntity>() {
                        @Override
                        public void onError(String message) {
                            baseCallBackListener.onFailed(message);
                        }

                        @Override
                        public void onSuccess(VideoResponseListEntity response) {
                            baseCallBackListener.onSuccess(response);
                        }
                    });
        }else {
            getServices()
                    .getBsyVideoList(pid, "2", 500)
                    .with(lifecycleOwner)
                    .subscribe(new DefaultObserver<VideoResponseListEntity>() {
                        @Override
                        public void onError(String message) {
                            baseCallBackListener.onFailed(message);
                        }

                        @Override
                        public void onSuccess(VideoResponseListEntity response) {
                            baseCallBackListener.onSuccess(response);
                        }
                    });
        }

    }


    /**
     * 获取视频播单列表
     *
     * @param pid 播单的id
     */
    public static void getBsyVideoList(String pid, int pageSize, BaseCallBackListener<VideoResponseListEntity> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        getServices()
                .getBsyVideoList(pid, "2", 500)
                .subscribe(new DefaultObserver<VideoResponseListEntity>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(VideoResponseListEntity response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });

    }

    /**
     * 获取视频播单列表 需要和界面做生命周期绑定
     *
     * @param lifecycleOwner 当前界面的生命周期监听对象
     * @param pid            播单的id
     */
    public static void getBsyVideoList(LifecycleOwner lifecycleOwner, String pid, int pageSize, BaseCallBackListener<VideoResponseListEntity> baseCallBackListener) {
        if (lifecycleOwner == null) {
            return;
        }
        baseCallBackListener.onHandlerStart();
        getServices()
                .getBsyVideoList(pid, "2", 500)
                .with(lifecycleOwner)
                .subscribe(new DefaultObserver<VideoResponseListEntity>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(VideoResponseListEntity response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });
    }


    /**
     * 获取真实的视频播放地址
     *
     * @param videoId 视频的id
     */
    public static void getRealVideoPath(String videoId, BaseCallBackListener<String> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        getServices()
                .getVideoPath(videoId)
                .subscribe(new DefaultObserver<String>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(String response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });
    }

    /**
     * 获取真实的视频播放地址
     *
     * @param owner                当前界面的生命周期监听对象
     * @param videoType 0 代表旧接口视频 1代表新接口视频
     * @param videoId              视频的id
     * @param baseCallBackListener 回调
     */
    public static void getRealVideoPath(LifecycleOwner owner, int videoType,String videoId, BaseCallBackListener<String> baseCallBackListener) {
        if (owner == null) {
            return;
        }
        baseCallBackListener.onHandlerStart();
        if (videoType == 0){
            getServices()
                    .getVideoPath(videoId)
                    .subscribe(new DefaultObserver<String>() {
                        @Override
                        public void onError(String message) {
                            baseCallBackListener.onFailed(message);
                        }

                        @Override
                        public void onSuccess(String response) {
                            baseCallBackListener.onSuccess(response);
                        }
                    });
        }else {
            getServices()
                    .getBsyVideoPath(videoId)
                    .subscribe(new DefaultObserver<String>() {
                        @Override
                        public void onError(String message) {
                            baseCallBackListener.onFailed(message);
                        }

                        @Override
                        public void onSuccess(String response) {
                            baseCallBackListener.onSuccess(response);
                        }
                    });
        }

    }


    /**
     * 获取真实的视频播放地址
     * 打包后台里的播单资源
     *
     * @param videoId 视频的id
     */
    public static void getRealBsyVideoPath(String videoId, BaseCallBackListener<String> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        getServices()
                .getBsyVideoPath(videoId)
                .subscribe(new DefaultObserver<String>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(String response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });
    }

    /**
     * 获取真实的视频播放地址
     * 打包后台里的播单资源
     *
     * @param owner                当前界面的生命周期监听对象
     * @param videoId              视频的id
     * @param baseCallBackListener 回调
     */
    public static void getRealBsyVideoPath(LifecycleOwner owner, String videoId, BaseCallBackListener<String> baseCallBackListener) {
        if (owner == null) {
            return;
        }
        baseCallBackListener.onHandlerStart();
        getServices()
                .getBsyVideoPath(videoId)
                .subscribe(new DefaultObserver<String>() {
                    @Override
                    public void onError(String message) {
                        baseCallBackListener.onFailed(message);
                    }

                    @Override
                    public void onSuccess(String response) {
                        baseCallBackListener.onSuccess(response);
                    }
                });
    }
}
